package com.example.endwork;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.dao.DBHelp;
import com.dao.DBoperate;
import com.items.person;

public class find extends AppCompatActivity {
    private EditText text;
    private Button b;
    private RadioGroup rg;
    private DBoperate dBoperate;
    private RadioButton chooseR;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_find);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        dBoperate=new DBoperate(new DBHelp(this));
        init();
        setL();
        setRG();
    }

    private void init(){
        this.text=(EditText) findViewById(R.id.STextText);
        this.b=(Button) findViewById(R.id.button);
        this.rg=findViewById(R.id.rg);
        //默认设置按照姓名选择
        chooseR=findViewById(R.id.radioButtonA);
        chooseR.setChecked(true);
    }

    private void setRG(){
        //设置修改监听器
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //时刻定位被选中的当选按钮
                chooseR=findViewById(i);
            }
        });
    }
    private void setL(){
        this.b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ans=text.getText().toString();
                String type=chooseR.getText().toString();
                //设置查找
                person p=dBoperate.search(ans,type);
                if(p==null){
                    Toast.makeText(find.this,"没有找到"+type+":"+ans,Toast.LENGTH_SHORT).show();
                }
                else{
                    //找到了，跳转到详情界面
                    Intent intent = new Intent(find.this, page2.class);
                    intent.putExtra("type", "A");
                    intent.putExtra("name", p.getName());
                    intent.putExtra("p1", p.getPhone_1());
                    intent.putExtra("p2", p.getPhone_2());
                    startActivity(intent);
                }
            }
        });
    }
}